/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/time.h"
/**
 * @brief Convert a local time structure to a time_t value.
 *
 * @param timeptr Pointer to a Ttm structure containing the local time to convert.
 * @return A time_t value representing the converted time.
 */
/**
 * @brief Convert a local time structure to a time_t value.
 *
 * @param timeptr Pointer to a Ttm structure containing the local time to convert.
 * @return A time_t value representing the converted time.
 */
time_t Smktime(struct Ttm *timeptr) {
    time_t seconds = 0;

    seconds += (timeptr->tm_year + 1900 - 1970) * 365 * 86400;
    if (timeptr->tm_year >= 1970) {
        seconds += (timeptr->tm_year + 1900 - 1969) / 4 * 86400;
    }

    for (int i = 0; i < timeptr->tm_mon; i++) {
        seconds += ((i % 2 == 0) ? 31 : 30) * 86400;
    }

    seconds += (timeptr->tm_mday - 1) * 86400;
    seconds += timeptr->tm_hour * 3600;
    seconds += timeptr->tm_min * 60;
    seconds += timeptr->tm_sec;

    return seconds;
}
